package priv.lirenhe.dissertation.component.search_engine.search_engine_executor;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.search.Query;

import priv.lirenhe.dissertation.component.search_engine.search_index.Indexer;
import priv.lirenhe.dissertation.component.search_engine.search_index.Queryer;
import priv.lirenhe.dissertation.component.search_engine.util.ProjectPath;
import priv.lirenhe.dissertation.component.search_engine.util.PropConfig;

public class SearchEngineExecutor {
	private static String indexPath = null;
	
	static{
		indexPath = (String) PropConfig.getPropConfig("params.properties").getProperty("indexPath");
		if(indexPath == null){
			String indexPath02 = ProjectPath.getFolderPathInProject("indexes");
			File file = new File(indexPath02);
			if(!file.exists()){
				file.mkdirs();
				indexPath = indexPath02;
			}
		}
	}
	
	public void searchEngineExecute(List<Map<String,String>> list_map_OfWebPage){
		System.out.println("searchEngineExecute检索引！");
		
		List<List<Field>> list_list_field = null;
		list_list_field = this.createIndexForWebPage(list_map_OfWebPage);
		Indexer indexer = null;
		if(indexPath != null){
			indexer = new Indexer(indexPath);
		}else{
			try {
				throw new Exception("索引路径没有找到！");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		List<Document> list_document = indexer.createDocuments(list_list_field);
		indexer.createIndex(list_document);
	}
	
	/**
	 * 
	 * @param list_map_OfWebPage
	 * @return
	 */
	public List<List<Field>> createIndexForWebPage(List<Map<String,String>> list_map_OfWebPage){
		List<List<Field>> list_list_field = null;
		if(list_map_OfWebPage != null){
			list_list_field = new ArrayList<List<Field>>();
			
			for(Map<String,String> map_OfWebPage : list_map_OfWebPage){
				
				List<Field> list_field = new ArrayList<Field>();
				String webPageTitle = null;
				String webPageRealurl = null;
				String webPageContentText = null;
				
				webPageTitle = map_OfWebPage.get("webPageTitle");
				webPageRealurl = map_OfWebPage.get("webPageRealurl");
				webPageContentText = map_OfWebPage.get("webPageContentText");
				
				if(webPageContentText != null){
					if(webPageTitle == null){
						webPageTitle = webPageContentText.substring(0,10);
						list_field.add(new Field("webPageTitle",webPageTitle,Field.Store.YES,Field.Index.ANALYZED));
					}else{
						list_field.add(new Field("webPageTitle",webPageTitle,Field.Store.YES,Field.Index.ANALYZED));
					}
					list_field.add(new Field("webPageRealurl",webPageRealurl,Field.Store.YES,Field.Index.ANALYZED));
					list_field.add(new Field("webPageContentText",webPageContentText,Field.Store.YES,Field.Index.ANALYZED_NO_NORMS));
				}
				
				System.out.println("入索引值="+webPageTitle);
				System.out.println("入索引值="+webPageRealurl);
				System.out.println("入索引值="+webPageContentText);
				
				
				list_list_field.add(list_field);
			}
		}
		return list_list_field;
		
	}
	
	/**
	 * 
	 * @param queryStr
	 * @param pageIndex
	 * @return
	 */
	public static List<Map<String,String>> searchWebPage(String queryStr,int pageIndex){
		Query query = null;
		List<Document> list_document = null;
		List<Map<String,String>> list_map_QueryValue = null;
		query = Queryer.getQuery(queryStr);
		Indexer indexer = new Indexer(indexPath);
		list_document = indexer.searchByQueryParser(query, pageIndex, 20);
		if(list_document != null && !list_document.isEmpty()){
			list_map_QueryValue = new ArrayList<Map<String,String>>();
			for(Document doc : list_document){
				Map<String,String> map = new HashMap<String,String>();
				map.put("webPageRealurl", doc.get("webPageRealurl"));
				map.put("webPageTitle", doc.get("webPageTitle"));
				map.put("webPageContentText", doc.get("webPageContentText"));
				list_map_QueryValue.add(map);
			}
		}
		return list_map_QueryValue;
	}
	
	public static void main(String[] args) {
		List<Map<String,String>> list_map_Value = null;
		list_map_Value = SearchEngineExecutor.searchWebPage("抱歉", 1);
		if(list_map_Value != null && !list_map_Value.isEmpty()){
			for(Map<String,String> map_Value :list_map_Value){
				System.out.println(map_Value.get("webPageRealurl"));
				System.out.println(map_Value.get("webPageTitle"));
				System.out.println(map_Value.get("webPageContentText"));
			}
		}else{
			System.out.println("没有检索到");
		}
	}
}
